package com.hl.card.modular.packBack.controller;

import com.hl.card.common.controller.BaseController;
import com.hl.card.common.persistence.model.JoinRebate;
import com.hl.card.common.persistence.model.JoinRebateType;
import com.hl.card.core.log.LogObjectHolder;
import com.hl.card.core.support.BeanKit;
import com.hl.card.modular.packBack.service.IJoinRebateService;
import com.hl.card.modular.packBack.service.JoinRebateTypeService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author huangpu
 * @since 2018-06-06
 */
@Controller
@RequestMapping("/joinRebate")
public class JoinRebateController extends BaseController {

    @Resource
    private JoinRebateTypeService joinRebateTypeService;

    @Resource
    private IJoinRebateService iJoinRebateService;

    private String PREFIX = "/packback/joinRebate/";

    /**
     *  主页
     */
    @RequestMapping("")
    public String index(){
        return PREFIX+"joinRebate.html";
    }

    /**
     * 跳转到添加
     */
    @RequestMapping(value = "/joinRebate_add")
    public Object joinRebateAdd(Model model){
        List<Map<String, Object>> list= joinRebateTypeService.selectList();
        model.addAttribute("joinrebatetypes",list);
        return PREFIX+"joinRebate_add.html";
    }

    /**
     * 跳转到修改销售利润管理
     */
    @RequestMapping(value = "/joinRebate_update/{joinRebateId}")
    public Object joinRebateUpdate(@PathVariable Integer joinRebateId, Model model){
        List<Map<String, Object>> list= joinRebateTypeService.selectList();
        JoinRebate joinRebate = iJoinRebateService.selectById(joinRebateId);
        Map<String, Object> joinRebatemap = BeanKit.beanToMap(joinRebate);
        model.addAttribute("joinRebate",joinRebatemap);
        model.addAttribute("joinrebatetypes",list);
        LogObjectHolder.me().set(joinRebate);
        return PREFIX+"joinRebate_edit.html";
    }

    /**
     * 获取列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        List<Map<String, Object>> list= iJoinRebateService.selectList();
        System.out.println(list);
        return list;
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/del",method = RequestMethod.POST)
    @ResponseBody
    public Object del(Integer id){
        iJoinRebateService.delete(id);
        return super.SUCCESS_TIP;
    }
    /**
     * 修改
     */
    @RequestMapping(value = "/update",method = RequestMethod.POST)
    public Object update(@Valid JoinRebate joinRebate){
        JoinRebateType joinRebateType = joinRebateTypeService.selectByCode(joinRebate.getJointypecode());
        joinRebate.setJointypename(joinRebateType.getJointypename());
        iJoinRebateService.updateById(joinRebate);
        return PREFIX+"joinRebate.html";
    }
    /**
     * 添加
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(JoinRebate joinRebate){
        JoinRebateType joinRebateType = joinRebateTypeService.selectByCode(joinRebate.getJointypecode());
        joinRebate.setJointypename(joinRebateType.getJointypename());
        iJoinRebateService.save(joinRebate);
        return super.SUCCESS_TIP;
    }



	
}
